package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsContentService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsContentDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsContent.
 */
@RestController
@RequestMapping("/api")
public class CmsContentResource {

    private final Logger log = LoggerFactory.getLogger(CmsContentResource.class);
        
    @Inject
    private CmsContentService cmsContentService;

    /**
     * POST  /cms-contents : Create a new cmsContent.
     *
     * @param cmsContentDTO the cmsContentDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsContentDTO, or with status 400 (Bad Request) if the cmsContent has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-contents")
    @Timed
    public ResponseEntity<CmsContentDTO> createCmsContent(@RequestBody CmsContentDTO cmsContentDTO) throws URISyntaxException {
        log.debug("REST request to save CmsContent : {}", cmsContentDTO);
        if (cmsContentDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsContent", "idexists", "A new cmsContent cannot already have an ID")).body(null);
        }
        CmsContentDTO result = cmsContentService.save(cmsContentDTO);
        return ResponseEntity.created(new URI("/api/cms-contents/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsContent", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-contents : Updates an existing cmsContent.
     *
     * @param cmsContentDTO the cmsContentDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsContentDTO,
     * or with status 400 (Bad Request) if the cmsContentDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsContentDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-contents")
    @Timed
    public ResponseEntity<CmsContentDTO> updateCmsContent(@RequestBody CmsContentDTO cmsContentDTO) throws URISyntaxException {
        log.debug("REST request to update CmsContent : {}", cmsContentDTO);
        if (cmsContentDTO.getId() == null) {
            return createCmsContent(cmsContentDTO);
        }
        CmsContentDTO result = cmsContentService.save(cmsContentDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsContent", cmsContentDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-contents : get all the cmsContents.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsContents in body
     */
    @GetMapping("/cms-contents")
    @Timed
    public List<CmsContentDTO> getAllCmsContents() {
        log.debug("REST request to get all CmsContents");
        return cmsContentService.findAll();
    }

    /**
     * GET  /cms-contents/:id : get the "id" cmsContent.
     *
     * @param id the id of the cmsContentDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsContentDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-contents/{id}")
    @Timed
    public ResponseEntity<CmsContentDTO> getCmsContent(@PathVariable Long id) {
        log.debug("REST request to get CmsContent : {}", id);
        CmsContentDTO cmsContentDTO = cmsContentService.findOne(id);
        return Optional.ofNullable(cmsContentDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-contents/:id : delete the "id" cmsContent.
     *
     * @param id the id of the cmsContentDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-contents/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsContent(@PathVariable Long id) {
        log.debug("REST request to delete CmsContent : {}", id);
        cmsContentService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsContent", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-contents?query=:query : search for the cmsContent corresponding
     * to the query.
     *
     * @param query the query of the cmsContent search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-contents")
    @Timed
    public List<CmsContentDTO> searchCmsContents(@RequestParam String query) {
        log.debug("REST request to search CmsContents for query {}", query);
        return cmsContentService.search(query);
    }


}
